package com.fz.song.controller.admin;


import com.alibaba.fastjson.JSONObject;
import com.fz.song.pojo.BlogBanner;
import com.fz.song.pojo.BlogInfo;
import com.fz.song.pojo.BlogLabel;
import com.fz.song.pojo.BlogType;
import com.fz.song.service.IBlogBannerService;
import com.fz.song.service.IBlogInfoService;
import com.fz.song.service.IBlogLableService;
import com.fz.song.service.IBlogTypeService;
import com.fz.song.util.DateUtil;
import com.fz.song.util.ResultObj;
import com.fz.song.util.UUIDUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("bloginfo")
public class BlogInfoController {


    @Autowired
    private IBlogInfoService blogInfoService;
    @Autowired
    private IBlogTypeService blogTypeService;
    @Autowired
    private IBlogLableService blogLableService;

    @RequestMapping("blog_info_list_vlew.html")
    public String blog_info_list_vlew_html() {
        return "admin/bloginfo/blog_info_list";
    }


    @RequestMapping("blog_info_list_data")
    public String blog_info_list_data(HttpServletRequest request, Integer page, @RequestParam(value = "limit", required = false, defaultValue = "10") int limit) {
        //获取数据
        PageInfo<BlogInfo> pageInfo = blogInfoService.getBlogInfoListData(page, limit);
        request.setAttribute("pageInfo", pageInfo);
        return "admin/bloginfo/blog_info_list_data";
    }



    @RequestMapping("go_bloginfo_saveorupdate.html")
    public String go_bloginfo_saveorupdate_html(HttpServletRequest request, String id) {
        if (null != id && !"".equals(id)) {
            BlogInfo blogInfo = blogInfoService.getBlogInfoByid(id);
            request.setAttribute("blogInfo", blogInfo);
        }
        //查询分类
        List<BlogType> typelist = blogTypeService.getBlogTypeList();
        request.setAttribute("typelist", typelist);
        //查询标签
        List<BlogLabel> labellist = blogLableService.getBlogLableList();
        request.setAttribute("labellist", labellist);
        return "admin/bloginfo/blog_info_save_or_update";
    }

    @RequestMapping("updateStatus")
    @ResponseBody
    public Object updateStatus(HttpServletRequest request, String id) {
        BlogInfo blogInfo = blogInfoService.getBlogInfoByid(id);
        Integer blogType = blogInfo.getBlogType();
        if (blogType == 0) {
            blogInfo.setBlogType(1);
            blogInfo.setReleasetime(DateUtil.getyMd());
            ResultObj resultObj = blogInfoService.release(blogInfo);
            return resultObj;
        } else {
            return ResultObj.error("已经发布过了");
        }

    }

    @RequestMapping("delBlogInfo")
    @ResponseBody
    public Object delBlogInfo(HttpServletRequest request, String id) {
        ResultObj resultObj = blogInfoService.delBlogInfo(id);
        return resultObj;

    }

    @RequestMapping("save_bloginfo")
    @ResponseBody
    public Object save_bloginfo(HttpServletRequest request, String jsonstr) {
        BlogInfo blogInfo = JSONObject.parseObject(jsonstr, BlogInfo.class);
        if (blogInfo.getId() == null || "".equals(blogInfo.getId())) {
            blogInfo.setId(UUIDUtil.getUUID());
            blogInfo.setBlogType(0);
            blogInfo.setCreatetime(DateUtil.getDateNow());
            blogInfo.setUpdatetime(DateUtil.getDateNow());
            blogInfo.setDel(0);
            blogInfo.setClicks(0);
            ResultObj resultObj=blogInfoService.saveBlogInfo(blogInfo);
            return resultObj;
        }else {
            BlogInfo blogInfo_db = blogInfoService.getBlogInfoByid(blogInfo.getId());
            blogInfo_db.setBlogName(blogInfo.getBlogName());
            blogInfo_db.setBlogType(0);
            blogInfo_db.setLabel(blogInfo.getLabel());
            blogInfo_db.setTypeid(blogInfo.getTypeid());
            blogInfo_db.setUpdatetime(DateUtil.getDateNow());
            blogInfo_db.setContent(blogInfo.getContent());
            ResultObj resultObj=blogInfoService.updateBlogInfo(blogInfo);
            return resultObj;
        }
    }

}
